<?php

namespace App\Http\Controllers\Manage;

use App\Helpers\ResponseData;
use App\Helpers\ServiceTrait;
use App\Http\Controllers\Controller;
use App\Models\QuotaionInformation;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

//客户报价信息
class QuotaionInformationController extends Controller
{
    use ServiceTrait,ResponseData;
    public function list(QuotaionInformation $model,Request $request){
        $user = $request->user();
        $size = $request->input('size');
        $model = $this->model($model,$user);
        $model = $model->with(['priceRange','customer','department','shop'=>function($query){
            $query->select('quotaion_information_id','shop_name','id','department_one_id','department_two_id','department_three_id','department_id');
        }]);
        if($request->filled('customer_id')){
            $model = $model->where('customer_id',$request->customer_id);
        }

        //权限管理
//        if($user->department_id!=1){
//            $model = $model->where('user_id',$user->id);
//        }
        $model = $model->orderByDesc('id');
        if($request->status=='all'){
            $model = $model->get();
        }else{
            $model = $model->paginate($size);
        }

        return $this->success($model);
    }
    public function store(Request $request,QuotaionInformation $model){
        $user = $request->user();
        if(!$request->filled('plateform_name')){
            return $this->failed('请选择平台');
        }
        $arr = explode(',',$request->plateform_name);
        if(count($arr)>0){
            $map = [];
            foreach ($arr as $k=>$v) {
                $map[$k]['plateform_id'] = $this->findPlateformId($v);
                $map[$k]['plateform_name'] = $v;
                $map[$k]['user_id'] = $user->id;
                $map[$k]['customer_id'] = $request->customer_id;
                $map[$k]['group_id'] = $request->group_id;
                $map[$k]['wx_group'] = $request->wx_group;
                $map[$k]['object_name'] = $request->object_name;
                $map[$k]['created_at'] = now();
                $map[$k]['updated_at'] = now();
                $map[$k]['department_quote_id'] = 0;
                $map[$k]['mark'] = $request->mark;
                $map[$k]['department_id'] = $user->department_id;
            }
            DB::table('quotaion_information')->insert($map);
        }

        $this->savePriceRange($request->group_id,$request);
        return $this->success('保存成功');
    }
    public function edit(QuotaionInformation $model,Request $request){
        $data = $model->find($request->id);
        return $this->success($data);
    }
    public function update(QuotaionInformation $model,Request $request){
        $requestData=$request->except('price_distance');
        $data = $model->find($request->id);
        if($request->filled('object_name')){
            $requestData['old_object_name'] = $data->object_name;
        }
        $data->update($requestData);
        return $this->success('修改成功');
    }
}
